# --------------------------------------------------
# -*- coding: utf-8 -*-
# Time: 2023/2/13 11:03
# Author: 沈凌鹏
# Version: 1.1
# Update content:1.对文档进行归类整理，且修改了文件路径说明
#                2.修改了将战斗信息保存到txt文档内的编码方式
# File: Hero.py
# Description: 管理英雄和英雄的战斗方式
# --------------------------------------------------

# 引入编码转换库
import codecs

# 基础英雄类
class hero:
    def __init__(self, hp, power):
        self.hp = hp
        self.power = power

    # 基本战斗函数
    def fight(self, enemy_hp, enemy_power):
        self.enemy_hp = enemy_hp
        self.enemy_power = enemy_power
        # 该变量用于计算回合数
        turn = 0
        # 体力比0大就继续战斗
        while self.hp > 0 and self.enemy_hp > 0:
            turn = turn + 1
            print("第", turn, "回合")
            # 战斗开始
            self.hp = self.hp - self.enemy_power
            self.enemy_hp = self.enemy_hp - self.power
            print("我方剩余HP：", self.hp, "敌方剩余HP", enemy_hp)
        # 判断胜负
        if self.hp > 0:
            print("英雄于第", turn, "回合获胜")
        elif self.enemy_hp > 0:
            print("敌人于第", turn, "回合获胜")
        else:
            print("平手")


# 继承基础英雄类，引入英雄速度
class Timo(hero):
    def __init__(self, hp, power, speed):
        super().__init__(hp, power)
        self.speed = speed

    # 重写Timo战斗函数，引入速度，判断先后手
    def fight(self, enemy_hp, enemy_power, enemy_speed):
        self.enemy_hp = enemy_hp
        self.enemy_power = enemy_power
        self.enemy_speed = enemy_speed
        # 该变量用于计算回合数
        turn = 0
        # 体力比0大就继续战斗
        while self.hp > 0 and self.enemy_hp > 0:
            turn = turn + 1
            print("第", turn, "回合")
            # 根据敌我速度来判断战斗的先后手
            if self.speed >= self.enemy_speed:
                # 只在第一回合显示先后手信息
                if turn == 1:
                    # 将先后手信息以utf-8的编码方式写入logs
                    with codecs.open("FightLogs.txt", 'a','utf-8') as fp:
                        fp.write("选择英雄：Timo\n")
                        fp.write("我方先手\n")
                    print("我方先手")
                # 战斗开始
                self.enemy_hp = self.enemy_hp - self.power
                # 根据先后手，当一方体力小于等于0，战斗结束，跳出循环
                if self.hp <= 0 or self.enemy_hp <= 0:
                    print("我方剩余HP：", self.hp, "敌方剩余HP", self.enemy_hp)
                    break
                self.hp = self.hp - self.enemy_power
            else:
                if turn == 1:
                    # 将先后手信息以utf-8的编码方式写入logs
                    with codecs.open("FightLogs.txt", 'a','utf-8') as fp:
                        fp.write("选择英雄：Timo\n")
                        fp.write("敌方先手\n")
                    print("敌方先手")
                self.hp = self.hp - self.enemy_power
                if self.hp <= 0 or self.enemy_hp <= 0:
                    print("我方剩余HP：", self.hp, "敌方剩余HP", self.enemy_hp)
                    break
                self.enemy_hp = self.enemy_hp - self.power
            print("我方剩余HP：", self.hp, "敌方剩余HP", self.enemy_hp)
        # 将回合数强制转化为字符类型
        t = str(turn)
        # 判断胜负，由于存在先后手，不存在平手情况
        if self.hp > 0:
            # 将结果以utf-8的编码方式写入logs
            with codecs.open("FightLogs.txt", 'a','utf-8') as fp:
                fp.write("英雄于第" + t + "回合获胜\n\n")
            print("英雄于第", turn, "回合获胜")
        elif self.enemy_hp > 0:
            # 将结果以utf-8的编码方式写入logs
            with codecs.open("FightLogs.txt", 'a','utf-8') as fp:
                fp.write("敌人于第" + t + "回合获胜\n\n")
            print("敌人于第", turn, "回合获胜")


# 继承基础英雄类，引入英雄速度，英雄防御
class Jinx(hero):
    def __init__(self, hp, power, speed, defences):
        super().__init__(hp, power)
        self.speed = speed
        self.defences = defences

    # 重写Jinx战斗函数，引入速度，判断先后手
    def fight(self, enemy_hp, enemy_power, enemy_speed, enemy_defences):
        self.enemy_hp = enemy_hp
        self.enemy_power = enemy_power
        self.enemy_speed = enemy_speed
        self.enemy_defences = enemy_defences
        # 该变量用于计算回合数
        turn = 0
        # 体力比0大就继续战斗
        while self.hp > 0 and self.enemy_hp > 0:
            turn = turn + 1
            print("第", turn, "回合")
            # 根据敌我速度来判断战斗的先后手
            if self.speed >= self.enemy_speed:
                # 只在第一回合显示先后手信息
                if turn == 1:
                    # 将先后手信息以utf-8的编码方式写入logs
                    with codecs.open("FightLogs.txt", 'a','utf-8') as fp:
                        fp.write("选择英雄：Jinx\n")
                        fp.write("我方先手\n")
                    print("我方先手")
                # 战斗开始
                self.enemy_hp = self.enemy_hp + self.enemy_defences - self.power
                # 根据先后手，当一方体力小于等于0，战斗结束，跳出循环
                if self.hp <= 0 or self.enemy_hp <= 0:
                    print("我方剩余HP：", self.hp, "敌方剩余HP", self.enemy_hp)
                    break
                self.hp = self.hp + self.defences - self.enemy_power
            else:
                if turn == 1:
                    # 将先后手信息以utf-8的编码方式写入logs
                    with codecs.open("FightLogs.txt", 'a','utf-8') as fp:
                        fp.write("选择英雄：Jinx\n")
                        fp.write("敌方先手\n")
                    print("敌方先手")
                self.hp = self.hp + self.defences - self.enemy_power
                if self.hp <= 0 or self.enemy_hp <= 0:
                    print("我方剩余HP：", self.hp, "敌方剩余HP", self.enemy_hp)
                    break
                self.enemy_hp = self.enemy_hp + self.enemy_defences - self.power
            print("我方剩余HP：", self.hp, "敌方剩余HP", self.enemy_hp)
        # 将回合数强制转化为字符类型
        t = str(turn)
        # 判断胜负，由于存在先后手，不存在平手情况
        if self.hp > 0:
            # 将结果以utf-8的编码方式写入logs
            with codecs.open("FightLogs.txt", 'a','utf-8') as fp:
                fp.write("英雄于第" + t + "回合获胜\n\n")
            print("英雄于第", turn, "回合获胜")
        elif self.enemy_hp > 0:
            # 将结果以utf-8的编码方式写入logs
            with codecs.open("FightLogs.txt", 'a','utf-8') as fp:
                fp.write("敌人于第" + t + "回合获胜\n\n")
            print("敌人于第", turn, "回合获胜")
